### Political Shock and International students: Estimating the ''Trump Effect"
### Mingsi Song###
### mingsi@tamu.edu##

rm(list = ls())

library(tidyverse)
library(readstata13)
library(gridExtra)


##### import in the dataset
trumpeffect <- read.dta13("trumpeffect.dta")


#### Table 2: Top Destinations During 2013- 2019
destinations <- trumpeffect %>%
  group_by(destination) %>%
  summarize(inflow = sum(number, na.rm = T)) %>%
  arrange(desc(inflow))

## Table 2 is the top 22 destinations during 2013 - 2019
Table2 <- head(destinations, 22)



##### Figure 1: International Student Enrollment from 2013 to 2019

## arrange the dataset by destination-year
host <- trumpeffect %>%
  group_by(cowc_host, year) %>%
  summarize(total  = sum(number, na.rm = T)) %>%
  arrange(total, year)




## United States
fig_usa <- subset(host, cowc_host == "USA") %>%
  ggplot(aes(x = year, y = total))+
  geom_line() +
  scale_x_continuous(name = "Year",
                     breaks = c(2013,
                                2016,2019),
                     labels = c("2013",
                                "2016",
                                "2019")) +
  scale_y_continuous(name = "Sum of Inflows",
                     breaks = c(800000,875000
                                ,950000),
                     labels = c("800000", "875000",
                                "950000"
                     )) +
  ggtitle("United States")



## United Kingdom
fig_ukg <- subset(host, cowc_host == "UKG") %>%
  ggplot(aes(x = year, y = total))+
  geom_line() +
  scale_x_continuous(name = "Year",
                     breaks = c(2013,
                                2016,2019),
                     labels = c("2013",
                                "2016",
                                "2019")) +
  scale_y_continuous(name = "Sum of Inflows",
                     breaks = c(420000,440000
                                ,460000),
                     labels = c("420000", "440000",
                                "460000"
                     )) +
  ggtitle("United Kingdom")


## Australia
fig_aus <- subset(host,cowc_host == "AUL") %>%
  ggplot(aes(x = year, y = total))+
  geom_line() +
  scale_x_continuous(name = "Year",
                     breaks = c(2013,
                                2016,2019),
                     labels = c("2013", 
                                "2016",
                                "2019")) +
  scale_y_continuous(name = "Sum of Inflows",
                     breaks = c(250000,350000
                                ,450000),
                     labels = c("250000", "350000",
                                "450000"
                     )) +
  ggtitle("Australia")


## Germany
fig_gmy <- subset(host, cowc_host == "GMY") %>%
  ggplot(aes(x = year, y = total))+
  geom_line() +
  scale_x_continuous(name = "Year",
                     breaks = c(2013,
                                2016,2019),
                     labels = c("2013",
                                "2016",
                                "2019")) +
  scale_y_continuous(name = "Sum of Inflows",
                     breaks = c(180000,230000
                                ,280000),
                     labels = c("180000", "230000",
                                "280000"
                     )) +
  ggtitle("Germany")



## France
fig_fra <- subset(host, cowc_host == "FRN") %>%
  ggplot(aes(x = year, y = total))+
  geom_line() +
  scale_x_continuous(name = "Year",
                     breaks = c(2013,
                                2016,2019),
                     labels = c("2013", 
                                "2016",
                                "2019")) +
  scale_y_continuous(name = "Sum of Inflows",
                     breaks = c(215000,225000
                                ,235000),
                     labels = c("215000", "225000",
                                "235000"
                     )) +
  ggtitle("France")



## Canada
fig_can <- subset(host, cowc_host == "CAN") %>%
  ggplot(aes(x = year, y = total))+
  geom_line() +
  scale_x_continuous(name = "Year",
                     breaks = c(2013,
                                2016,2019),
                     labels = c("2013",
                                "2016",
                                "2019")) +
  scale_y_continuous(name = "Sum of Inflows",
                     breaks = c(150000,200000,
                                250000),
                     labels = c("150000", "200000",
                                "250000"
                     )) +
  ylab("Sum of Inflows")+
  ggtitle("Canada")

## Top 5 Besides US
other5 <- trumpeffect %>%
  group_by(other5, year) %>%
  summarize(total  = sum(number, na.rm = T)) %>%
  arrange(total, year)



fig_other5 <- subset(other5, other5 == 1) %>%
  ggplot(aes(x = year, y = total))+
  geom_line() +
  scale_x_continuous(name = "Year",
                     breaks = c(2013,
                                2016,2019),
                     labels = c("2013", 
                                "2016",
                                "2019")) +
  scale_y_continuous(name = "Sum of Inflows",
                     breaks = c(1200000,1400000,
                                1700000),
                     labels = c("1200000", "1400000",
                                "1700000"
                     )) +
  ggtitle("Top 5 Besides US")






## Top 10 Besides US
other10 <- trumpeffect %>%
  group_by(other10, year) %>%
  summarize(total  = sum(number, na.rm = T)) %>%
  arrange(total, year)




fig_other10 <- subset(other10, other10 == 1) %>%
  ggplot(aes(x = year, y = total))+
  geom_line() +
  scale_x_continuous(name = "Year",
                     breaks = c(2013,
                                2016,2019),
                     labels = c("2013",
                                "2016",
                                "2019")) +
  scale_y_continuous(name = "Sum of Inflows",
                     breaks = c(1600000,2000000,
                                2400000),
                     labels = c("16000000", "2000000",
                                "2400000"
                     )) +
  ggtitle("Top 10 Besides US")


## Top 20 Besides US
other20 <- trumpeffect %>%
  group_by(other20, year) %>%
  summarize(total  = sum(number, na.rm = T)) %>%
  arrange(total, year)




fig_other20 <- subset(other20, other20 == 1) %>%
  ggplot(aes(x = year, y = total))+
  geom_line() +
  scale_x_continuous(name = "Year",
                     breaks = c(2013,
                                2016,2019),
                     labels = c("2013",
                                "2016",
                                "2019")) +
  scale_y_continuous(name = "Sum of Inflows",
                     breaks = c(2000000,2450000,
                                2900000),
                     labels = c("2000000", "2450000",
                                "2900000"
                     )) +
  ggtitle("Top 20 Besides US")



## All Destinations Besides US
allothers <- trumpeffect %>%
             subset(ccode_host != 2) %>%
             group_by(year) %>%
  summarize(total  = sum(number, na.rm = T)) %>%
  arrange(total, year)

             

fig_allother <- allothers %>%
  ggplot(aes(x = year, y = total))+
  geom_line() +
  scale_x_continuous(name = "Year",
                     breaks = c(2013,
                                2016,2019),
                     labels = c("2013", 
                                "2016",
                                "2019")) +
  scale_y_continuous(name = "Sum of Inflows",
                     breaks = c(2500000,3250000
                                ,4000000),
                     labels = c("2500000", "3250000",
                                "4000000"
                     )) +
  ggtitle("All Destinations Besides US")


### Generate Figure 1
g1 <- arrangeGrob(fig_usa, fig_ukg, fig_aus, fig_gmy, 
                  fig_fra, fig_can, ncol = 3)
g2 <- arrangeGrob(fig_other5, fig_other10, 
                  fig_other20, fig_allother, ncol = 3)

pdf(file = "figure1.pdf",   # The directory you want to save the file in
    width = 14, 
    height = 12) 

grid.arrange(g1, g2)


dev.off()

################## Figure 2, 3, 4: Pairwise DID Estimates
rm(list = ls())
# install the "countrycode" package if not
# install.packages("countrycode")
library(countrycode)
library(tidyverse)
library(foreign)


### import the pairwise DID coefficients
pair <- read_csv("pairwise.csv")
pair$Country <- countrycode(pair$country, "cowc",
                            "country.name", warn = TRUE, 
                            nomatch = NA)
## Generate Figure 2
pair1 <- subset(pair, ci_l < 0 & ci_u <0)
pair1$Country <- factor(pair1$Country, 
                        levels = pair1$Country[order(pair1$b, 
                                                     decreasing = FALSE)])

figure2 <- pair1 %>%
  ggplot(aes(x = Country, y = b)) +
  geom_point() +
  xlab("Country") +
  ylab("Coefficient") +
  geom_segment(aes(y = ci_l, yend = ci_u,
                   x = Country, xend = Country))+
  geom_hline(yintercept = 0, color = "red", linetype = "dashed")+
  coord_flip()



pdf(file = "figure2.pdf",   # The directory you want to save the file in
    width = 8, 
    height = 6)

figure2

dev.off()


## Generate Figure 3
pair3 <- subset(pair, ci_l < 0 & ci_u > 0)
pair3$Country <- factor(pair3$Country, 
                        levels = pair3$Country[order(pair3$b, 
                                decreasing = FALSE)])


figure3 <- pair3 %>%
  ggplot(aes(x = Country, y = b)) +
  geom_point() +
  xlab("Country") +
  ylab("Coefficient") +
  geom_segment(aes(y = ci_l, yend = ci_u,
                   x = Country, xend = Country))+
  geom_hline(yintercept = 0, color = "red", linetype = "dashed")+
  coord_flip()

pdf(file = "figure3.pdf",   # The directory you want to save the file in
    width = 8, 
    height = 6)

figure3

dev.off()



## Generate figure 4
pair2 <- subset(pair, ci_l > 0 & ci_u > 0)
pair2$Country <- factor(pair2$Country, 
                        levels = pair2$Country[order(pair2$b, 
                                    decreasing = FALSE)])

figure4 <- pair2 %>%
  ggplot(aes(x = Country, y = b)) +
  geom_point() +
  xlab("Country") +
  ylab("Coefficient") +
  geom_segment(aes(y = ci_l, yend = ci_u,
                   x = Country, xend = Country))+
  geom_hline(yintercept = 0, color = "red", linetype = "dashed")+
  coord_flip()



pdf(file = "figure4.pdf",   # The directory you want to save the file in
    width = 8, 
    height = 6)

figure4

dev.off()







